package com.vmj.classfile.entity.attribute;

import com.vmj.classfile.ClassReader;

/**
 * Created by yangshoujun on 2017/8/6 下午3:35.
 */
public class LocalVariableTableAttribute extends AbstractAttribute {

    private LocalVariableTableEntry[] localVariableTableEntries;

    public LocalVariableTableAttribute(ClassReader classReader) {
        super(classReader);
    }

    public void parse() {
        int localVariableTableLength = getClassReader().readTwoByte();
        localVariableTableEntries = new LocalVariableTableEntry[localVariableTableLength];
        for(int i = 0;i < localVariableTableLength;i++) {
            LocalVariableTableEntry entry = new LocalVariableTableEntry();
            entry.setStartPc(getClassReader().readTwoByte());
            entry.setLength(getClassReader().readTwoByte());
            entry.setNameIndex(getClassReader().readTwoByte());
            entry.setDescriptorIndex(getClassReader().readTwoByte());
            entry.setIndex(getClassReader().readTwoByte());
            localVariableTableEntries[i] = entry;
        }
    }

    class LocalVariableTableEntry {
        private int startPc;
        private int length;
        private int nameIndex;
        private int descriptorIndex;
        private int index;

        public int getStartPc() {
            return startPc;
        }

        public int getLength() {
            return length;
        }

        public int getNameIndex() {
            return nameIndex;
        }

        public int getDescriptorIndex() {
            return descriptorIndex;
        }

        public int getIndex() {
            return index;
        }

        public void setStartPc(int startPc) {
            this.startPc = startPc;
        }

        public void setLength(int length) {
            this.length = length;
        }

        public void setNameIndex(int nameIndex) {
            this.nameIndex = nameIndex;
        }

        public void setDescriptorIndex(int descriptorIndex) {
            this.descriptorIndex = descriptorIndex;
        }

        public void setIndex(int index) {
            this.index = index;
        }
    }
}
